﻿using Microsoft.Extensions.DependencyInjection;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIA.Foundation;
using VIA.Presentation;
using VIA.SiMoJi.Domain.Electronics;
using VIA.SiMoJi.Domain.Inspection;

namespace VIA.SiMoJi.Presentation
{
    public class EquipmentRunningStateModel : Model
    {
        private readonly IServiceProvider _service;
        private readonly IDispatcherService _dispatcherService;
        private readonly Inspector _inspector;
        GongLiaoRunningStateModel _gongLiaoModel;
        BanYunRunningStateModel _banYunModel;
        SiMoRunningStateModel _siMoModel;
        public GongLiaoRunningStateModel GongLiaoModel
        {
            get => _gongLiaoModel;
            set => SetProperty(ref _gongLiaoModel, value);
        }
        public BanYunRunningStateModel BanYunModel
        {
            get => _banYunModel;
            set => SetProperty(ref _banYunModel, value);
        }
        public SiMoRunningStateModel SiMoModel
        {
            get => _siMoModel;
            set => SetProperty(ref _siMoModel, value);
        }
        public EquipmentRunningStateModel(IServiceProvider service)
        {
            _service = service;
            _inspector = service.GetService<Inspector>();
            _dispatcherService = service.GetService<IDispatcherService>();
        }
        public void Refresh()
        {
            #region 供料工位
            _gongLiaoModel = new GongLiaoRunningStateModel();
            _gongLiaoModel.IsPiDaiAxisDone1 = !_inspector.GongLiaoStation.Axis_PiDai1.IsDone;
            _gongLiaoModel.IsPiDaiAxisDone2 = !_inspector.GongLiaoStation.Axis_PiDai2.IsDone;
            _gongLiaoModel.LoadSenser = Electronic.Default.DI20_入料口光电.Value;
            _gongLiaoModel.ZProductSenser = Electronic.Default.DI19_入料防追尾光电.Value;
            _gongLiaoModel.TopBotCylinder1 = Electronic.Default.DI36_入料前后拍板上下气缸1伸出磁开.Value ? true : false;
            _gongLiaoModel.TopBotCylinder2 = Electronic.Default.DI38_入料前后拍板上下气缸2伸出磁开.Value ? true : false;
            _gongLiaoModel.FrontCylinder1 = Electronic.Default.DI32_入料前后拍板气缸1伸出磁开.Value ? true : false;
            _gongLiaoModel.FrontCylinder2 = Electronic.Default.DI34_入料前后拍板气缸2伸出磁开.Value ? true : false;
            _gongLiaoModel.LeftRightCylinder1 = Electronic.Default.DI28_入料左右拍板气缸1伸出磁开.Value ? true : false;
            _gongLiaoModel.LeftRightCylinder2 = Electronic.Default.DI30_入料左右拍板气缸2伸出磁开.Value ? true : false;
            _gongLiaoModel.ArriveDecSenser = Electronic.Default.DI39_入料到位减速光电.Value;
            _gongLiaoModel.ArriveSenser = Electronic.Default.DI21_入料到位光电.Value;
            _gongLiaoModel.ResistCylinder1 = Electronic.Default.DI23_入料到位挡缸1伸出磁开.Value;
            _gongLiaoModel.ResistCylinder2 = Electronic.Default.DI25_入料到位挡缸2伸出磁开.Value;
            _gongLiaoModel.UnloadSenser = Electronic.Default.DI22_出料口光电.Value;
            #endregion

            #region 搬运工位
            _banYunModel = new BanYunRunningStateModel();
            _banYunModel.NozzleVacuo1 = Electronic.Default.DI40_取放料真空表1.Value;
            _banYunModel.NozzleVacuo2 = Electronic.Default.DI41_取放料真空表2.Value;
            _banYunModel.NozzleVacuo3 = Electronic.Default.DI42_取放料真空表3.Value;
            _banYunModel.NozzleVacuo4 = Electronic.Default.DI43_取放料真空表4.Value;
            _banYunModel.NozzleVacuo5 = Electronic.Default.DI44_取放料真空表5.Value;
            _banYunModel.NozzleVacuo6 = Electronic.Default.DI45_取放料真空表6.Value;
            _banYunModel.NozzleVacuo7 = Electronic.Default.DI46_取放料真空表7.Value;
            #endregion

            #region 撕膜工位
            _siMoModel = new SiMoRunningStateModel();
            _siMoModel.Feeder1.YaMoVacuo = Electronic.Default.DI86_飞达1放置到位.Value;
            _siMoModel.Feeder1.CloseSenser = Electronic.Default.DI83_飞达1抽屉闭合光电.Value;
            _siMoModel.Feeder1.PtBandFullCheckSenser = Electronic.Default.DI88_飞达1满料检测.Value;
            _siMoModel.Feeder1.PtBandIsNotCheckSenser = Electronic.Default.DI85_飞达1料带有无检测.Value;

            _siMoModel.Feeder2.YaMoVacuo = Electronic.Default.DI90_飞达2放置到位.Value;
            _siMoModel.Feeder2.CloseSenser = Electronic.Default.DI84_飞达2抽屉闭合光电.Value;
            _siMoModel.Feeder2.PtBandFullCheckSenser = Electronic.Default.DI92_飞达2满料检测.Value;
            _siMoModel.Feeder2.PtBandIsNotCheckSenser = Electronic.Default.DI89_飞达2料带有无检测.Value;

            _siMoModel.JiaZhua1.JiaZhuaOpenSenser = Electronic.Default.DI80_夹爪张开光纤1.Value;
            _siMoModel.JiaZhua1.JiaZhuaCloseSenser = Electronic.Default.DI79_夹爪闭合光纤1.Value;

            _siMoModel.JiaZhua1.JiaMoSenser1 = Electronic.Default.DI65_夹爪膜检测光纤11.Value;
            _siMoModel.JiaZhua1.JiaMoSenser2 = Electronic.Default.DI66_夹爪膜检测光纤12.Value;
            _siMoModel.JiaZhua1.JiaMoSenser3 = Electronic.Default.DI67_夹爪膜检测光纤13.Value;
            _siMoModel.JiaZhua1.JiaMoSenser4 = Electronic.Default.DI68_夹爪膜检测光纤14.Value;
            _siMoModel.JiaZhua1.JiaMoSenser5 = Electronic.Default.DI69_夹爪膜检测光纤15.Value;
            _siMoModel.JiaZhua1.JiaMoSenser6 = Electronic.Default.DI70_夹爪膜检测光纤16.Value;
            _siMoModel.JiaZhua1.JiaMoSenser7 = Electronic.Default.DI71_夹爪膜检测光纤17.Value;

            _siMoModel.JiaZhua2.JiaZhuaOpenSenser = Electronic.Default.DI82_夹爪张开光纤2.Value;
            _siMoModel.JiaZhua2.JiaZhuaCloseSenser = Electronic.Default.DI81_夹爪闭合光纤2.Value;

            _siMoModel.JiaZhua2.JiaMoSenser1 = Electronic.Default.DI72_夹爪膜检测光纤21.Value;
            _siMoModel.JiaZhua2.JiaMoSenser2 = Electronic.Default.DI73_夹爪膜检测光纤22.Value;
            _siMoModel.JiaZhua2.JiaMoSenser3 = Electronic.Default.DI74_夹爪膜检测光纤23.Value;
            _siMoModel.JiaZhua2.JiaMoSenser4 = Electronic.Default.DI75_夹爪膜检测光纤24.Value;
            _siMoModel.JiaZhua2.JiaMoSenser5 = Electronic.Default.DI76_夹爪膜检测光纤25.Value;
            _siMoModel.JiaZhua2.JiaMoSenser6 = Electronic.Default.DI77_夹爪膜检测光纤26.Value;
            _siMoModel.JiaZhua2.JiaMoSenser7 = Electronic.Default.DI78_夹爪膜检测光纤27.Value;

            #endregion
            _dispatcherService.InvokeAsync(() => 
            {
                this.RaisePropertyChanged(nameof(GongLiaoModel));
                this.RaisePropertyChanged(nameof(BanYunModel));
                this.RaisePropertyChanged(nameof(SiMoModel));
            });
        }
    }
    [AddINotifyPropertyChangedInterface]
    public class GongLiaoRunningStateModel
    {
        /// <summary>
        /// 皮带轴1
        /// </summary>
        public bool IsPiDaiAxisDone1 { get; set; }
        /// <summary>
        /// 皮带轴2
        /// </summary>
        public bool IsPiDaiAxisDone2 { get; set; }
        /// <summary>
        /// 入料光电
        /// </summary>
        public bool LoadSenser { get; set; }
        /// <summary>
        /// Z向有无料光电
        /// </summary>
        public bool ZProductSenser { get; set; }
        /// <summary>
        /// 上下气缸1
        /// </summary>
        public bool TopBotCylinder1 { get; set; }
        /// <summary>
        /// 上下气缸2
        /// </summary>
        public bool TopBotCylinder2 { get; set; }
        /// <summary>
        /// 前后气缸1
        /// </summary>
        public bool FrontCylinder1 { get; set; }
        /// <summary>
        /// 前后气缸2
        /// </summary>
        public bool FrontCylinder2 { get; set; }
        /// <summary>
        /// 左右气缸1
        /// </summary>
        public bool LeftRightCylinder1 { get; set; }
        /// <summary>
        /// 左右气缸2
        /// </summary>
        public bool LeftRightCylinder2 { get; set; }
        /// <summary>
        /// 到位减速光电
        /// </summary>
        public bool ArriveDecSenser { get; set; }
        /// <summary>
        /// 到位光电
        /// </summary>
        public bool ArriveSenser { get; set; }
        /// <summary>
        /// 阻挡气缸1
        /// </summary>
        public bool ResistCylinder1 { get; set; }
        /// <summary>
        /// 阻挡气缸2
        /// </summary>
        public bool ResistCylinder2 { get; set; }
        /// <summary>
        /// 出料光电
        /// </summary>
        public bool UnloadSenser { get; set; }
    }

    [AddINotifyPropertyChangedInterface]
    public class BanYunRunningStateModel
    {
        public bool NozzleVacuo1 { get; set; }
        public bool NozzleVacuo2 { get; set; }
        public bool NozzleVacuo3 { get; set; }
        public bool NozzleVacuo4 { get; set; }
        public bool NozzleVacuo5 { get; set; }
        public bool NozzleVacuo6 { get; set; }
        public bool NozzleVacuo7 { get; set; }
    }
    [AddINotifyPropertyChangedInterface]
    public class SiMoRunningStateModel
    {
        public FeederRunningStateModel Feeder1 { get; set; }
        public FeederRunningStateModel Feeder2 { get; set; }
        public JiaZhuaRunningStateModel JiaZhua1 { get; set; }
        public JiaZhuaRunningStateModel JiaZhua2 { get; set; }
        public SiMoRunningStateModel()
        {
            Feeder1 = new FeederRunningStateModel();
            Feeder2 = new FeederRunningStateModel();
            JiaZhua1 = new JiaZhuaRunningStateModel();
            JiaZhua2 = new JiaZhuaRunningStateModel();
        }
    }
    [AddINotifyPropertyChangedInterface]
    public class FeederRunningStateModel
    {
        /// <summary>
        /// 压膜真空
        /// </summary>
        public bool YaMoVacuo { get; set; }
        /// <summary>
        /// 闭合光电
        /// </summary>
        public bool CloseSenser { get; set; }
        /// <summary>
        /// 料带满检测
        /// </summary>
        public bool PtBandFullCheckSenser { get; set; }
        /// <summary>
        /// 料带有无检测
        /// </summary>
        public bool PtBandIsNotCheckSenser { get; set; }
    }
    [AddINotifyPropertyChangedInterface]
    public class JiaZhuaRunningStateModel
    {
        /// <summary>
        /// 夹爪张开光电
        /// </summary>
        public bool JiaZhuaOpenSenser { get; set; }
        /// <summary>
        /// 夹爪关闭光电
        /// </summary>
        public bool JiaZhuaCloseSenser { get; set; }
        /// <summary>
        /// 夹膜光电1
        /// </summary>
        public bool JiaMoSenser1 { get; set; }
        /// <summary>
        /// 夹膜光电2
        /// </summary>
        public bool JiaMoSenser2 { get; set; }
        /// <summary>
        /// 夹膜光电3
        /// </summary>
        public bool JiaMoSenser3 { get; set; }
        /// <summary>
        /// 夹膜光电4
        /// </summary>
        public bool JiaMoSenser4 { get; set; }
        /// <summary>
        /// 夹膜光电5
        /// </summary>
        public bool JiaMoSenser5 { get; set; }
        /// <summary>
        /// 夹膜光电6
        /// </summary>
        public bool JiaMoSenser6 { get; set; }
        /// <summary>
        /// 夹膜光电7
        /// </summary>
        public bool JiaMoSenser7 { get; set; }
    }
}
